var app = new Vue({
    el: '.box',
    data: {
        tableData: '',//分类数据
        dialogFormVisible: false,//显示编辑界面
        dialogFormVisible1: false,//显示新建分类
        formLabelWidth: '70px',//宽度
        form: '',//编辑内容
        form1: {
            name: '',
            beizhu: '',
            flag: true,
            url: '',
            head: 1042,
            token: (sessionStorage.getItem('token'))
        },//新建内容
    },
    methods: {
        del(obj) {
            var arr = { ids: this.tableData[obj.$index].Id, head: 1041, token: (sessionStorage.getItem('token')) }
            this.$confirm('此操作将删除该分类, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                del_item(arr);
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
            // 删除
        },
        new_msgs(obj) {
            if (!this.form1.name) {
                this.$message.error('请输入分类名');
                return false;
            };
            this.form1.flag1 = this.form1.flag ? 1 : 0;
            new_item(this.form1);
        },
        save(obj, type) {
            if (type) {
                this.dialogFormVisible = true;
                this.tableData[obj.$index].head = 1043;
                this.tableData[obj.$index].token = (sessionStorage.getItem('token'));
                this.form = this.tableData[obj.$index];
            } else {
                this.form.flag = this.form.flag1 ? 1 : 0;
                console.log(this.form);
                svae_item(this.form);
            };
        },
        clear() {
            this.dialogFormVisible1 = true;
            this.form1.name = '';
            this.form1.beizhu = '';
            this.form1.flag = true;
            this.form1.url = '';
        }
    },
});



function svae_item(obj) {
    var loading = app.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
    });
    app.dialogFormVisible = false;
    axios({
        method: "POST",
        baseURL: '../../api/tag.php',
        data: 'data=' + JSON.stringify(obj)
    }).then(function (res) {
        loading.close();
        if (res.data.code === '200') {
            app.$message({
                message: '操作成功~',
                type: 'success'
            });
            select_item();
        } else if (res.data.code === '500') {
            window.location.href = res.data.href;
        } else {
            app.$message({
                type: 'info',
                message: res.data.data.msg
            });
            console.log(res);
        };
    }).catch(function (err) {
        console.log(err);
    });
};//编辑分类

function new_item(obj) {
    var loading = app.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
    });
    app.dialogFormVisible1 = false;
    axios({
        method: "POST",
        baseURL: '../../api/tag.php',
        data: 'data=' + JSON.stringify(obj)
    }).then(function (res) {
        loading.close();
        if (res.data.code === '200') {
            app.$message({
                message: '操作成功~',
                type: 'success'
            });
            select_item();
        } else if (res.data.code === '500') {
            window.location.href = res.data.href;
        } else {
            app.$message({
                type: 'info',
                message: res.data.data.msg
            });
            console.log(res);
        };
    }).catch(function (err) {
        console.log(err);
    });
};//新建

function del_item(obj) {
    var loading = app.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
    });
    axios({
        method: "POST",
        baseURL: '../../api/tag.php',
        data: 'data=' + JSON.stringify(obj)
    }).then(function (res) {
        loading.close();
        if (res.data.code === '200') {
            app.$message({
                message: '操作成功~',
                type: 'success'
            });
            select_item();
        } else if (res.data.code === '500') {
            window.location.href = res.data.href;
        } else {
            app.$message({
                type: 'info',
                message: res.data.data.msg
            });
            console.log(res);
        };
    }).catch(function (err) {
        console.log(err);
    });
};//删除


select_item();
function select_item() {
    var loading = app.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
    });
    axios({
        method: "POST",
        baseURL: '../../api/tag.php',
        data: 'data=' + JSON.stringify({ head: 1040, token: (sessionStorage.getItem('token')) })
    }).then(function (res) {
        loading.close();
        if (res.data.code === '200') {
            for (var i = 0; i < res.data.data.length; i++) {
                res.data.data[i].flag1 = (res.data.data[i].flag - 0) === 1 ? true : false;
                res.data.data[i].flag2 = (res.data.data[i].flag - 0) === 1 ? true : false;
                res.data.data[i].ids = res.data.data[i].Id;
            };
            app.tableData = res.data.data;

        } else if (res.data.code === '500') {
            window.location.href = res.data.href;
        } else {
            console.log(res);
        };
    }).catch(function (err) {
        console.log(err);
    });
};//获取分类